﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyAlbum.Model;
using System.Xml.Linq;
using MyAlbum.Utility;


namespace MyAlbum.Data.Xml
{
    /// <summary>
    /// Manages the replies for the entries. This is done separately from the entries 
    ///  repository as the replies are added and edited on different times and places and 
    ///  not when the entry is created/edited
    /// </summary>
    public class ReplyRepository : XmlRepository, IRepository<MyAlbum.Model.Reply>
    {
        private static object locker = "locker";
        public ReplyRepository(string rootPath)
            : base(rootPath, "Replies")
        {
        }
        #region IRepository<Entry> Members

        public List<MyAlbum.Model.Reply> GetItems()
        {
            return this.Document.Descendants("Reply").Select(s => new MyAlbum.Model.Reply()
            {
                Id = (s.Attribute("Id").Value.Trim()),
                Subject = s.Element("Subject").Value.Trim(),
                EntryId = (s.Element("EntryId").Value),
                IsApproved = (bool)s.Element("IsApproved"),
                Body = s.Element("Body").Value.Trim(),
                TimeStamp = (DateTime)s.Element("TimeStamp"),
                Email = s.Element("Email").Value.Trim(),
                UserName = s.Element("UserName").Value.Trim()
            }).ToList();
        }

        public IEnumerable<MyAlbum.Model.Reply> GetItemsForEntry(string entryId)
        {
            return this.Document.Descendants("Reply").Where(r => (r.Element("EntryId").Value) == entryId).Select(s => new MyAlbum.Model.Reply()
            {
                Id = (s.Attribute("Id").Value),
                Subject = s.Element("Subject").Value.Trim(),
                EntryId = (s.Element("EntryId").Value),
                IsApproved = (bool)s.Element("IsApproved"),
                Body = s.Element("Body").Value.Trim(),
                TimeStamp = (DateTime)s.Element("TimeStamp"),
                Email = s.Element("Email").Value.Trim(),
                UserName = s.Element("UserName").Value.Trim()
            });
        }

        public void AddItem(MyAlbum.Model.Reply item)
        {
            Assert.IsNotNull(item);
            lock (locker)
            {
                item.Id = Guid.NewGuid().ToString();
                XElement a = new XElement("Reply",
                    new XAttribute("Id", item.Id),
                    new XElement("IsApproved", item.IsApproved),
                    new XElement("EntryId", item.EntryId),
                    new XElement("UserName", item.UserName ?? string.Empty),
                    new XElement("Subject", item.Subject ?? string.Empty),
                    new XElement("TimeStamp", item.TimeStamp),
                    new XElement("Email", item.Email ?? string.Empty),
                    new XElement("Body", item.Body ?? string.Empty));
                Document.Element("Replies").Add(a);
                this.Save();
            }
        }

        public void DeleteItem(MyAlbum.Model.Reply item)
        {
            Assert.IsNotNull(item);
            lock (locker)
            {
                var saved = (from e in Document.Descendants("Reply") where (e.Attribute("Id").Value) == item.Id select e).FirstOrDefault();
                if (saved != null)
                {
                    saved.Remove();
                    this.Save();
                }
            }
        }

        public void UpdateItem(MyAlbum.Model.Reply item)
        {
            Assert.IsNotNull(item);
            lock (locker)
            {
                var saved = (from e in Document.Descendants("Reply") where (e.Attribute("Id").Value) == item.Id select e).FirstOrDefault();
                if (saved != null)
                {
                    saved.Element("IsApproved").ReplaceWith(new XElement("IsApproved", item.IsApproved));
                    saved.Element("EntryId").ReplaceWith(new XElement("EntryId", item.EntryId));
                    saved.Element("UserName").ReplaceWith(new XElement("UserName", item.UserName ?? string.Empty));
                    saved.Element("Subject").ReplaceWith(new XElement("Subject", item.Subject ?? string.Empty));
                    saved.Element("Email").ReplaceWith(new XElement("Email", item.Email ?? string.Empty));
                    saved.Element("Body").ReplaceWith(new XElement("Body", item.Body ?? string.Empty));
                    this.Save();
                }
            }
        }


        public MyAlbum.Model.Reply GetItemById(string id)
        {
            return this.Document.Descendants("Reply").Where(s => (s.Attribute("Id").Value) == id).Select(s => new MyAlbum.Model.Reply()
            {
                Id = (s.Attribute("Id").Value),
                Subject = s.Element("Subject").Value.Trim(),
                EntryId = s.Element("EntryId").Value,
                IsApproved = (bool)s.Element("IsApproved"),
                Body = s.Element("Body").Value.Trim(),
                TimeStamp = (DateTime)s.Element("TimeStamp"),
                Email = s.Element("Email").Value.Trim(),
                UserName = s.Element("UserName").Value.Trim()
            }).SingleOrDefault();
        }

        #endregion
    }
}
